package main;

import java.awt.Color;
import java.awt.Graphics;

public class Relationship {
	Pixel a;
	Pixel b;
	double length;
	double elasticity;
	boolean invisible;
	boolean ultraInvisible;
	public Manager manager;

	public Relationship(Pixel aPix, Pixel bPix, int length, double elasticity) {
		a = aPix;
		b = bPix;
		this.length = length;
		this.elasticity = elasticity;
		invisible = false;
	}

	public int draw(Graphics g) {
		g.setColor(Color.black);
		if (ultraInvisible){
			return 0;
		}
		if (elasticity > 5) {
			g.setColor(Color.green);
		}
		if (invisible) {
			if (manager.draw) {
				if (elasticity > 5) {
					g.setColor(new Color(180, 255, 180));
				} else {
					g.setColor(Color.yellow);
				}
			} else {
				return 0;
			}
		}
		g.drawLine((int) a.x, (int) a.y, (int) b.x, (int) b.y);
		return 0;
	}

	public void act() {
		double distance = Math.sqrt(Math.pow(b.x - a.x, 2)
				+ Math.pow(b.y - a.y, 2));
		double dX = b.x - a.x;
		double dY = b.y - a.y;
		double lower = Math.sqrt(dX * dX + dY * dY);
		double xDif, yDif;
		xDif = dX * (distance - length) / lower;
		yDif = dY * (distance - length) / lower;
//		if (a.clicked) {
//			a.x += xDif * b.weight/ (2 * elasticity);
//			a.y += yDif * b.weight/ (2 * elasticity);
//		}
			a.xSpeed += xDif * b.weight/ (2 * elasticity);
			a.ySpeed += yDif * b.weight/ (2 * elasticity);
//		if (b.clicked) {
//			b.x -= xDif * a.weight/ (2 * elasticity);
//			b.y -= yDif * a.weight/ (2 * elasticity);
//		}
			b.xSpeed -= xDif * a.weight/ (2 * elasticity);
			b.ySpeed -= yDif * a.weight/ (2 * elasticity);
	}
}
